package monopoli.communication;

import java.util.ArrayList;


public class Communication implements CommunicationInterface {

    //Attributes
    private static Host host;
    
    
    //Constructors
    public Communication () {
            host = new Host();
    }
    
    public Communication (long id, String hostIp, int host_port, String serverIp, int server_port) {
            host = new Host();
            this.setHost(hostIp, host_port);
            this.setInitializationServer(serverIp, server_port);
    }

    
    
    
    //Public Method
    @Override
    public boolean setHost(String hostIp, int host_port) {

        //Aggiornamento dati interni e binding
        RMIAddress rmiA = new RMIAddress(hostIp, host_port, "Host");
        Player p = new Player();
        p.setRMIA(rmiA);
        boolean res = host.setMyPlayer(p);

        return res;
        
    }

    @Override
    public RMIAddress getHost() {

        return host.getMyPlayer().getRMIA();
    }

    @Override
    public boolean setInitializationServer(String serverIp, int server_port) {
        
        return host.lookupInitializationServer(serverIp, server_port);
    }

    @Override
    public String pingNextHost() {

        long res_ping = host.pingNextHost();
        if (res_ping < 0)
            return new String("Errore durante la chiamata di pingNextHost()");
        return new String(Long.toString(res_ping) + "ms");
    }

    @Override
    public String pingInitializationServer() {

        long res_ping = host.pingInitializationServer();
        if (res_ping < 0)
            return new String("[DBG_com] Errore durante la chiamata di pingInitializationServer()");
        return new String(Long.toString(res_ping) + "ms");
    }




    

    @Override
    public boolean richiestaPartecipaPartita(Integer idGiocatore) {
        return host.requestGame(idGiocatore);
    }

    @Override
    public boolean passaTurno () {
        return host.sendTkn();
    }

    @Override
    public void lanciaDadi() {
    }

    @Override
    public boolean notificaMuoviPedina (int pedina,int casella) {
        ArrayList<Object> al = new ArrayList<Object>();
        al.add(new Integer(pedina));
        al.add(new Integer(casella));
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_MOVE_PEDINA, 0, al, 1);
    }

    @Override
    public boolean notificaNome(int giocatore, String name) {
        ArrayList<Object> al = new ArrayList<Object>();
        al.add(new Integer(giocatore));
        al.add(name);
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_NAME, 0, al, 1);
    }

    @Override
    public boolean notificaPedina(int giocatore, int idPedina) {
        ArrayList<Object> al = new ArrayList<Object>();
        al.add(new Integer(giocatore));
        al.add(new Integer(idPedina));
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_PEDINA, 0, al, 1);
    }

    @Override
    public boolean notificaPossessoProprieta(int giocatore, int idProprieta) {
        ArrayList<Object> al = new ArrayList<Object>();
        al.add(new Integer(giocatore));
        al.add(new Integer(idProprieta));
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_GET_PROPERTY, 0, al, 1);
    }

    @Override
    public boolean notificaAcquistoProprieta (int giocatore,int idProprieta) {
        ArrayList<Object> al = new ArrayList<Object>();
        al.add(new Integer(giocatore));
        al.add(new Integer(idProprieta));
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_BUY_PROPERTY, 0, al,1);
    }

    @Override
    public boolean notificaCostruzioneEdificio (int giocatore,int casella,int edificio) {
        ArrayList<Object> al = new ArrayList<Object>();
        al.add(new Integer(giocatore));
        al.add(new Integer(casella));
        al.add(new Integer(edificio));
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_BUILD_BUILDING, 0, al, 1);
    }
    
    @Override
    public boolean notificaPagamento (int amount,int giocatore1,int giocatore2) {
        ArrayList<Object> al = new ArrayList<Object>();
        al.add(new Integer(amount));
        al.add(new Integer(giocatore1));
        al.add(new Integer(giocatore2));
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_PAYMENT, 0, al, 1);
    }

    @Override
    public boolean notificaRitiro () {
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_WITHDRAWAL, 0, null, 1);
    }

    @Override
    public boolean notificaIpoteca () {
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_MORTGAGE, 0, null, 1);
    }

    @Override
    public boolean notificaFinePartita () {
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_END_GAME, 0, null, 1);
    }

    @Override
    public boolean notificaAstaAvviata () {
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_AUCTION_STARTED, 0, null, 1);
    }

    @Override
    public boolean notificaCadutaGiocatore () {
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_DEATH_PLAYER, 0, null, 1);
    }

    @Override
    public boolean notificaMsg (String s) {
        ArrayList<Object> al = new ArrayList<Object>();
        al.add(s);
        return host.sendMsg(Message.TYPE_NOTIFY, Message.SUBTYPE_NOTIFY_MSG, 0, al, 1);
    }

    @Override
    public void rilanciaAsta () {
    }

    @Override
    public void rinunciaAsta () {
    }

    @Override
    public void proponiScambio () {
    }

    

    
}

